package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.dto.AgentGrade;
import tech.aistar.service.IAgentService;
import tech.aistar.service.IES;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/agent")
public class AgentGradeController {

    @Autowired
    private IAgentService agentService;

    @Autowired
    private IES ies;

    //经纪人初始页面
    @RequestMapping("/select")
    public String selectAgentHome(HttpServletRequest request) {
        String pageNow = request.getParameter("pageNow");
        pageNow = pageNow == null ? "1" : pageNow;
        PageInfo<AgentGrade> pageInfo = agentService.findByPage(null, Integer.valueOf(pageNow), 4);
        request.setAttribute("page", pageInfo);
        return "/home_page/jingjirenliebiao";
    }

    //所有经纪人查询并分页
    @RequestMapping("/select/{sea}/{pageNow}")
    public String selectAgent(@PathVariable("sea") String sea, @PathVariable("pageNow") String pageNow, HttpServletRequest request) {
        pageNow = pageNow == null ? "1" : pageNow;
        PageInfo<AgentGrade> pageInfo = agentService.findByPage(null, Integer.valueOf(pageNow), 4);
        request.setAttribute("page", pageInfo);
        return "/home_page/jingjirenliebiao";
    }


    //绑定经纪人(合租)
    @RequestMapping("/bindWhole/{id}")
    public String bindAgentWhole(@PathVariable("id") String id, HttpServletRequest request) {
        request.setAttribute("agent_id", id);
        return "/home_page/postHomeWhole";
    }

    //绑定经纪人(整租)
    @RequestMapping("/bindTogether/{id}")
    public String bindAgentTogether(@PathVariable("id") String id, HttpServletRequest request) {
        request.setAttribute("agent_id", id);
        return "/home_page/postHomeTogether";
    }

    //经纪人搜索
    @RequestMapping("/select/{sea}")
    public String selectAgentSearch(@PathVariable("sea") String sea, HttpServletRequest request) {

        //调用ES搜索方法
        List<Map<String, Object>> agents = ies.searchEsAgent(sea);

        //固定页数只查一条
//        String pageNow = "1";
//        PageInfo<AgentGrade> pageInfo = agentService.findByPage(sea, Integer.valueOf(pageNow), 4);

        request.setAttribute("zw","none");
        request.setAttribute("agents", agents);
        return "/home_page/jingjirenliebiao";
    }

}